<?php
/**
 * 文章分类控制器
 * @param index - 列表
 */
namespace Manage\Controller;
use Common\Library\Category;
use Common\Library\Page;
class ArticleCategoryController extends CommonController{
	private $msg=array();
	private $Model;
	function _initialize(){
		parent::_initialize();
		$this->Model=D('ArticleCategory');
	}
	public function main(){
		$this->redirect('index');
	}
	//列表
	public function index(){
		if(!$this->checkPermission('ArticleCategory')) $this->jump();
		$data=array();
		if(I('title')) $data['title']=I('title');
		if(I('status')) $data['status']=I('status');
		$list=$this->Model->getArticleCategories($data);
		$url['p']=I('get.p','');
		$list=Category::unlimitedForLevel($list,'',0,0,'article_category_id','parent_id','title');
		$count=count($list);
		$Page=new Page($count,C('CONFIG_LIMIT_MANAGE'));
		$Page->setConfig('header',L('page_header'));
		$Page->setConfig('theme',L('page_theme'));
		$show=$Page->show();
		$list=array_splice($list,$Page->firstRow,$Page->listRows);
		$this->assign('list',$list);
		$this->assign('url',$url);
		$this->assign('page',$show);
		$this->display();
	}
	/**
	 * 修改及新增
	 */
	public function oper(){
		$article_category_id=I('get.article_category_id',0);
		if(!$article_category_id && !$this->checkPermission('AddArticleCategory')) $this->jump();	
		$languages=get_langs();
		$article_category=$this->Model->getArticleCategory($article_category_id);
		if($article_category_id && empty($article_category)) $this->jump(L('error_404'));
		if(IS_POST){
			$article_category=I('post.');
			if($this->vilidateForm()){
				$this->Model->operArticleCategory($article_category,$article_category_id);
				session('success',$article_category_id?L('success_edit'):L('success_add'));
				$url['p']=I('get.p','');
				$this->redirect('index',$url);
			}else{
				if(!isset($this->msg['warning'])) $this->msg['warning']=L('error_warning');
			}
		}
		$article_category['thumb']=image_resize($article_category['image'],100,100);
		$article_category['placeholder']=image_resize();
		$this->assign('languages',$languages);
		$data_filter=array(
			'article_category_id'	=>	$article_category_id
			);		
		$article_categories=$this->Model->getArticleCategories($data_filter);
		$article_categories=Category::unlimitedForLevel($article_categories,'',0,0,'article_category_id','parent_id','title');
		$this->assign('article_categories',$article_categories);
		$this->assign('msg',$this->msg);
		$this->assign('article_category',$article_category);
		$this->display('oper');
	}
	/**
	 * 
	 */
	public function del(){
		if($this->checkPermission('DeleteProduct')){
			if($this->vilidateDelete()){
				foreach (I('post.selected') as $article_category_id) {
					$count += $this->Model->del($article_category_id);
				}
				if($count){
					session('success',L('success_delete',array('count'=>$count)));
				}
			}else{
				session('danger',L('error_delete'));
			}
		}else{
			session('danger',L('error_not_permission'));
		}

		$this->redirect('index');
	}
	/**
	 * 表单验证
	 */
	private function vilidateForm(){
		$article_category_id=I('get.article_category_id',0,'trim');
		if($article_category_id && !$this->checkPermission('EditArticleCategory')){
			$this->msg['warning']=L('error_not_permission');
			return !$this->msg;
		}
		$data=I('post.');
		foreach($data['article_category_description'] as $language_id => $value){
			if(mb_strlen($value['title'])<2 || mb_strlen($value['title'])>255){
				$this->msg['title'][$language_id]=L('error_title');
			}
			if(mb_strlen($value['meta_title'])>255){
				$this->msg['meta_title'][$language_id]=L('error_meta_title');
			}
			if(mb_strlen($value['meta_description'])>255){
				$this->msg['meta_description'][$language_id]=L('error_meta_description');
			}
			if(mb_strlen($value['meta_keyword'])>255){
				$this->msg['meta_keyword'][$language_id]=L('error_meta_keyword');
			}
		}
		return !$this->msg;
	}
	/**
	 * 删除前的操作，检查该分类下是否有内容，如果有则不予删除
	 */
	private function vilidateDelete(){
		$map['article_category_id']=array('in',I('post.selected'));
		$return=M('Article')->where($map)->select();
		return empty($return);
	}
	/**
	 * 自动完成
	 */
	public function autocomplete(){
		$json=array();
		$title=I('get.article_category_title','','htmlspecialchars,trim');
		$filter_data['status']=1;
		$article_categories=$this->Model->getArticleCategories($filter_data);
		$article_categories=Category::unlimitedForLevel($article_categories,'',0,0,'article_category_id','parent_id','title');
		if($title){
			foreach ($article_categories as $article_category){
				$in_html=strripos($article_category['html'],$title);
				$in_title=strripos($article_category['title'],$title);
				if(($in_html !== false) || ($in_title !== false ) ){
					$json[]=array(
						'title'	=>	$article_category['title'],
						'article_category_id'			=>	$article_category['article_category_id'],
						'html'			=>	$article_category['html']
						);
				}
			}
		}else{
			$json=$article_categories;
		}
		$json=array_splice($json,0,8);
		$this->ajaxReturn($json,'JSON');
	}
}